//////////////////////////////////////
//// ASKIMA - COLLABORATOR PROFILE
//// FORMATIONS MANAGEMENT FUNCTIONS
//////////////////////////////////////

// 1. DISPLAY MODE

/**
 * 
 * @param sourceButton
 * @param formationID
 */

function expandFormation(sourceButton, formationID) {
	var nTr = sourceButton.parentNode.parentNode;
	if (sourceButton.src.match('details_close')) {
		/* This row is already open - close it */
		sourceButton.src = 'http://datatables.net/examples/examples_support/details_open.png';
		formationTable.fnClose(nTr);
	} else {
		/* Open this row */
		sourceButton.src = 'http://datatables.net/examples/examples_support/details_close.png';
		displayFormationDetails(formationTable, nTr, formationID);
	}
}

/**
 * 
 * @param oTable
 * @param nTr
 * @param formationID
 */

function displayFormationDetails(oTable, nTr, formationID) {
	var aData = oTable.fnGetData(nTr);
	$.getJSON("formationSkillsToJSON", {
		formationID : formationID,
	}, function(data) {
		var sOut = "<ul>";
		$.each(data, function(i, val) {
			var name = val.name;
			sOut += "<li>" + name + "</li>";
		});
		sOut += "</ul>";
		formationTable.fnOpen(nTr, sOut, 'details');
	});
}

/**
 * Expands the panel in order to add a formation
 */

function showAddFormationPanel() {
	$("#formationStartDate").datepicker({
		dateFormat : 'dd/mm/yy',
		changeMonth : true,
		changeYear : true,
		showAnim : 'fold',
		yearRange : '1950:2050'
	});
	$("#formationEndDate").datepicker({
		dateFormat : 'dd/mm/yy',
		changeMonth : true,
		changeYear : true,
		showAnim : 'fold',
		yearRange : '1950:2050'
	});
	$("#add_formation_panel").dialog({
		title : 'Ajouter une formation au profil',
		width : 600,
		close : function(event, ui) {
			clearAddFormationForm();
		}
	});

	addFormationSkillInput();
}

/**
 * Add a formation skill input
 */

function addFormationSkillInput() {
	var counter = new Object();
	counter.value = formationInputID;
	addSkillInput(selectedFormationSkills, "formationSkillInput_", counter,
			"formation_skills_panel");
	formationInputID = counter.value;
}

/**
 * Removes a formation skill input
 */

function removeFormationSkillInput() {
	var counter = new Object();
	counter.value = formationInputID;
	removeSkillInput(selectedFormationSkills, counter, "formationSkillInput_",
			"formations_skills_panel");
	formationInputID = counter.value;
}

/**
 * Submit the add formation form in POST method and send information to
 * controller
 */

function submitFormationForm() {

	var inputName;
	// Iterate over the add formation skills panel children and retrieves their
	// contents
	for ( var i = 0; i <= formationInputID; i++) {
		inputName = "formationSkillInput_" + i;
		selectedFormationSkills[i] = $(
				'#formation_skills_panel > #' + inputName).val();
	}

	$("#formationSkillsList").val(selectedFormationSkills.join("|"));
	$("#addFormationForm").submit();
}

/**
 * Clear the add formation form
 */

function clearAddFormationForm() {
	$('#formationTitle').val("");
	$('#formationStartDate').val("");
	$('#formationEndDate').val("");
	$('#formationEstablishment').val("");
	$('input[id^="formationSkillInput"]').prev().remove();
	$('input[id^="formationSkillInput"]').remove();
	formationInputID = -1;
	selectedFormationSkills = [];
}

/**
 * Delete a formation
 * 
 * @param formationID
 *            The id of the formation to delete
 */

function deleteFormation(formationID, formationTitle) {
	$(
			"<p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>Voulez vous supprimer la formation : '"
					+ formationTitle + "' ?</p>").dialog({
		resizable : true,
		modal : true,
		buttons : {
			"Valider" : function() {
				$('<p></p>').load(delFormation({
					formationID : formationID
				}), function() {
					alert("La formation à été supprimée !");
					window.location.reload();
				});
			},
			"Annuler" : function() {
				$(this).dialog("close");
			}
		}
	});
}

// 2. EDIT MODE

/**
 * Show the update formation panel (and fill inputs with formation data)
 */

function showUpdateFormationPanel(formationID) {

	$.getJSON("formationToJSON", {
		formationID : formationID,
	}, function(data) {

		// Set the hidden field value (to JAVA side)
		$("#updateFormationForm > #updateFormationID").val(formationID);

		// Mission title
		$("#updateFormationForm > #updateFormationTitle").val(data.title);

		// Date pickers management
		$("#updateFormationForm > #updateFormationStartDate").datepicker({
			dateFormat : 'dd/mm/yy',
			changeMonth : true,
			changeYear : true,
			showAnim : 'fold',
			yearRange : '1950:2050'
		});

		$("#updateFormationForm > #updateFormationEndDate").datepicker({
			dateFormat : 'dd/mm/yy',
			changeMonth : true,
			changeYear : true,
			showAnim : 'fold',
			yearRange : '1950:2050'
		});
		$("#updateFormationForm > #updateFormationStartDate").datepicker(
				"setDate", new Date(data.startDate));
		$("#updateFormationForm > #updateFormationEndDate").datepicker(
				"setDate", new Date(data.endDate));

		// Establishment
		$("#updateFormationForm > #updateFormationEstablishment").val(
				data.establishment);

		// Skills management
		var updateSkills = data.formationSkills;
		for ( var int = 0; int < updateSkills.length; int++) {
			addUpdateFormationSkillInput();
			$("#formationUpdateSkillInput_" + formationUpdateInputID).val(
					updateSkills[int].name);
		}

		// Show the dialog
		$("#update_formation_panel").dialog({
			title : 'Modifier Formation',
			width : 620,
			close : function(event, ui) {
				clearUpdateFormationForm();
			}
		});
	});
}

/**
 * 
 */

function addUpdateFormationSkillInput() {
	var counter = new Object();
	counter.value = formationUpdateInputID;
	addSkillInput(selectedUpdateFormationSkills, "formationUpdateSkillInput_",
			counter, "update_formation_skills_panel");
	formationUpdateInputID = counter.value;
}

/**
 * 
 */

function removeUpdateFormationSkillInput() {
	var counter = new Object();
	counter.value = formationUpdateInputID;
	removeSkillInput(selectedUpdateFormationSkills, counter,
			"formationUpdateSkillInput_", "update_formation_skills_panel");
	formationUpdateInputID = counter.value;
}

/**
 * 
 */

function updateFormation() {

	var inputName;
	// Iterate over the update formation skills panel children and retrieves their
	// contents
	for ( var i = 0; i <= formationUpdateInputID; i++) {
		inputName = "formationUpdateSkillInput_" + i;
		selectedUpdateFormationSkills[i] = $(
				'#update_formation_skills_panel > #' + inputName).val();
	}

	var selSkills = selectedUpdateFormationSkills.join("|");
	$("#updateFormationSkillsList").val(selSkills);
	$("#updateFormationForm").submit();
}

/**
 * Clear the mission update form
 */

function clearUpdateFormationForm() {
	$("#updateFormationForm > #updateFormationID").val("");
	$("#updateFormationForm > #updateFormationTitle").val("");
	$("#updateFormationForm > #updateFormationStartDate").val("");
	$("#updateFormationForm > #updateFormationEndDate").val("");
	$("#updateFormationForm > #updateFormationEstablishment").val("");
	$(
			'#update_formation_skills_panel > input[id^="formationUpdateSkillInput_"]')
			.prev().remove();
	$(
			'#update_formation_skills_panel > input[id^="formationUpdateSkillInput_"]')
			.remove();
	formationUpdateInputID = -1;
	selectedUpdateFormationSkills = [];
}
